/*
 * ConectarDialog
 * 
 * Pollo Verde Software 2006
 * 
 * Este programa se distribuye segun la licencia GPL v.2 o posteriores y no
 * tiene garantias de ningun tipo. Puede obtener una copia de la licencia GPL o
 * ponerse en contacto con la Free Software Foundation en http://www.gnu.org
 */
package org.pvs.palitos.spgui;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.*;

import org.pvs.palitos.SuperPalitos;

/**
 * @author Angel Luis Calvo Ortega
 */
public class ConectarDialog extends JDialog {
	private static final long serialVersionUID = 3258416131596628789L;
	private JButton aceptar, cancelar;
  private JLabel jLName, jLColor, jLDir, jLPuerto;
  private JTextField jTFName, jTFDir, jTFPuerto;
  private ColorBox colorBox;
  private JPanel panel;
  private int state, port;
  private static ConectarDialog instancia;
  
  public static final int INICIO = 0;
  public static final int CONECTANDO = 1;
  
  /** Creates a new instance of JDConectar */
  private ConectarDialog(Frame owner) {
    super(owner, "Conectar con...", false);
    
    state = INICIO;
    
    setResizable(false);
    
    panel = new JPanel();
    panel.setBorder(new EmptyBorder(5,5,5,5));
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    //
    JPanel p = new JPanel(new GridLayout(2, 2, 10, 2));
    p.setBorder(new TitledBorder(new EtchedBorder(), " Jugar con "));
    jLDir = new JLabel("Direccion del rival:");
    p.add(jLDir);
    jTFDir = new JTextField();
    p.add(jTFDir);
    jLPuerto = new JLabel("Puerto");
    p.add(jLPuerto);
    jTFPuerto = new JTextField(String.valueOf(SuperPalitos.getInstance().getPuerto()));
    p.add(jTFPuerto);
    panel.add(p);
    
    p = new JPanel(new GridLayout(2, 2, 10, 2));
    p.setBorder(new TitledBorder(new EtchedBorder(), " Jugador "));
    jLName = new JLabel("Nombre:");
    p.add(jLName);
    jTFName = new JTextField(SuperPalitos.getInstance().getJ1Name());
    //textFieldUser1.setFont(new Font("Arial", Font.BOLD, 12));
    p.add(jTFName);
    jLColor = new JLabel("Color:");
    p.add(jLColor);
    colorBox = new ColorBox(SPFrame.COLORS);
    colorBox.setSelectedColor(SuperPalitos.getInstance().getJ1Color());
    p.add(colorBox);
    panel.add(p);
    
    p = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    cancelar = new JButton("Cancelar", SPFrame.II_CANCEL);
    cancelar.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        cancelar(e);
      }
    });
    p.add(cancelar);
    aceptar = new JButton("Aceptar", SPFrame.II_ACCEPT);
    aceptar.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        aceptar(e);
      }
    });
    p.add(aceptar);
    panel.add(p);
    
    getContentPane().add(panel, BorderLayout.CENTER);
     
    pack();
    setLocationRelativeTo(owner);
  }
  
  /**
   *
   */
  public static void iniciar(Frame owner) {
    instancia = new ConectarDialog(owner);
    instancia.setVisible(true);
  }
  
  /**
   * 
   * @param s
   */
  public static void error(String s) {
    if(instancia.state == CONECTANDO) {
      JOptionPane.showMessageDialog(null, s, "Error", JOptionPane.ERROR_MESSAGE);
      instancia.state = INICIO;
      //instancia.setTitle(s);
      instancia.jTFDir.setEnabled(true);
      instancia.jTFName.setEnabled(true);
      instancia.colorBox.setEnabled(true);
      instancia.aceptar.setEnabled(true);
    }
  }
  
  /**
   * 
   *
   */
  public static void fin() {
    instancia.dispose();
  }
  
  /**
   * 
   * @param e
   */
  private void aceptar(ActionEvent e) {
    if(datosOk()) {
      setTitle("Conectando...");
      jTFDir.setEnabled(false);
      jTFName.setEnabled(false);
      colorBox.setEnabled(false);
      aceptar.setEnabled(false);
      
      SuperPalitos.getInstance().setJ1Name(jTFName.getText());
      SuperPalitos.getInstance().setJ1Color(colorBox.getSelectedColor());
      
      //Todo conectar
      state = CONECTANDO;
      SuperPalitos.getInstance().conectar(jTFDir.getText(), port);
    } else {
      JOptionPane.showMessageDialog(this, "Datos incorrectos", "Error", JOptionPane.ERROR_MESSAGE);
    }
  }
  
  /**
   * 
   * @param e
   */
  private void cancelar(ActionEvent e) {
    if(state == CONECTANDO) {
      state = INICIO;
      setTitle("JDConectar");
      jTFDir.setEnabled(true);
      jTFName.setEnabled(true);
      colorBox.setEnabled(true);
      aceptar.setEnabled(true);
    } else {
      dispose();
    }
  }
  
  /**
   * 
   * @return
   */
  private boolean datosOk() {
    try {
      port = Integer.parseInt(jTFPuerto.getText());
      return jTFDir.getText().length() > 0 && jTFName.getText().length() > 0 && port > 1023 && port < 65535;
    } catch(NumberFormatException exp) {
      return false;
    }
  }
}
